package com.kgc.scd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.HotKey;

import com.kgc.scd.mapper.HotKeyMapper;
import com.kgc.scd.mapper.SearchHistoryMapper;
import com.kgc.scd.util.RedisUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;

@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.kgc.scd.mapper")
@EnableFeignClients
public class BlmallServerWzy8106Application implements ApplicationRunner {
    @Autowired(required = false)
    private HotKeyMapper hotKeyMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired(required = false)
    private SearchHistoryMapper searchHistoryMapper;
    @Autowired
    private RedisUtils redisUtils;

    public static void main(String[] args) {
        SpringApplication.run(BlmallServerWzy8106Application.class, args);
    }

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        // 将热词存入redis
        Set<Object> range = redisTemplate.opsForZSet().range("hotKey", 0, -1);
        if (range.size()!=0){
            redisTemplate.opsForZSet().removeRange("hotKey",0,-1);
        }
        //QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
        //wrapper.eq("hotkey_key","hotKey");
        List<HotKey> hotKeyList = hotKeyMapper.selectALLHotKeyList();
        hotKeyList.forEach(hotKey1 -> redisTemplate.opsForZSet().incrementScore("hotKey",hotKey1.getHotkeyValue(),hotKey1.getHotkeyScore()));



        //
//        // 将搜索历史添加到redis中
//        List<SearchHistory> searchHistories = searchHistoryMapper.selectList(null);
//        searchHistories.forEach(searchHistory1 -> redisTemplate.opsForList().leftPush(searchHistory1.getHistoryKey(), searchHistory1.getHistoryValue()));
    }
}
